package com.lonwin.inspectionmanagement.app.utils;


import java.util.ArrayList;

import java.util.HashSet;
import java.util.Iterator;

import java.util.List;

import java.util.Set;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import android.widget.ListView;

import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.App;
import com.jess.arms.utils.ArmsUtils;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.GpsUpdataInfoBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HhkqLhAliasBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SearLocusBean;
import com.lonwin.inspectionmanagement.mvp.presenter.HomePresenter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.HomepopuwindowAdaptert;
import com.lonwin.inspectionmanagement.widget.BlurryPopupWindow;


/**
 * 各种数据处理
 * 
 * @author liuxuechao
 * 
 */
public class DataUtil {

	/**
	 * 文件图片使用地址
	 * @return
	 */
	public static String getFileUrl(ConfigBean configBean)
	{
//		return "http://hzz.lonhcloud.cn/hzxh/upload";
//		http://oss.lonhcloud.net/lhcenter/api/v1/oss

		return AppUtil.convert2HttpUrl(configBean.getServerInfo().getOSS_SERVER())+"lhcenter/api/v1/oss";
	}
	/**
	 * 对接金牛的url
	 * @param configBean
	 * @return
	 */
	 public static String getJNUrl(ConfigBean configBean){
//	 	return Api.BSAE_YW_URL;

		 return AppUtil.convert2HttpUrl(configBean.getLoginInfo().getLhproject().getServerForBusiness());
	 }
    /**
     * 得到业务 的url
     * @param configBean
     * @return
     */
	public static String getYWUrl(ConfigBean configBean)
	{
		return AppUtil.convert2HttpUrl(configBean.getServerInfo().getAPI_SERVER());
	}

	/**
	 * 得到数据中心的baseurl
	 * @param configBean
	 * @return
	 */
	public static String getSJUrl(ConfigBean configBean)
	{
		return AppUtil.convert2HttpUrl(configBean.getServerInfo().getGPS_SERVER());
	}


	private static final double EARTH_RADIUS = 6378137.0;

	/**
	 * 计算未完成和的里程
	 * @param gpsPoints
	 * @return
	 */
	public static double getMileage_2(List<SearLocusBean.DataBean.GpsPointsBean> gpsPoints)
	{
		if (gpsPoints==null||gpsPoints.isEmpty()||gpsPoints.size()==1)
		{
			return 0;
		}
		double temMileag=0.0;
		for (int i=0;i<gpsPoints.size()-1;i++)
		{
			temMileag=temMileag+getDistance(gpsPoints.get(i).getLongitude(),gpsPoints.get(i).getLatitude(),gpsPoints.get(i+1).getLongitude(),gpsPoints.get(i+1).getLatitude());
		}
		return temMileag;
	}

	/**
	 * 计算两个经纬度之间的距离
	 * @param gpsUpdataInfoBeans
	 * @return
	 */
	public static double getMileage(List<GpsUpdataInfoBean> gpsUpdataInfoBeans)
	{
		if (gpsUpdataInfoBeans==null||gpsUpdataInfoBeans.isEmpty()||gpsUpdataInfoBeans.size()==1)
		{
			return 0;
		}
		double temMileag=0.0;
		for (int i=0;i<gpsUpdataInfoBeans.size()-1;i++)
		{
			temMileag=temMileag+getDistance(gpsUpdataInfoBeans.get(i).getLongitude(),gpsUpdataInfoBeans.get(i).getLatitude(),gpsUpdataInfoBeans.get(i+1).getLongitude(),gpsUpdataInfoBeans.get(i+1).getLatitude());
		}
		return temMileag;
	}
	// 返回单位是米
	public static double getDistance(double longitude1, double latitude1,
									 double longitude2, double latitude2) {
		double Lat1 = rad(latitude1);
		double Lat2 = rad(latitude2);
		double a = Lat1 - Lat2;
		double b = rad(longitude1) - rad(longitude2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(Lat1) * Math.cos(Lat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public static String formtTime_new(int time){
		int hour=(int) Math.floor(time/(60*60));
		int minute=(int) Math.floor((time-(hour*60*60))/60);
		int second=time-((hour*60*60+minute*60));
		return hour+"小时"+minute+"分"+second+"秒";
	}
	public static String formtTime(int time){
		int hour=(int) Math.floor(time/(60*60));
		int minute=(int) Math.floor((time-(hour*60*60))/60);
		int second=time-((hour*60*60+minute*60));
		return "巡河时长："+hour+"小时"+minute+"分"+second+"秒";
	}
	/**
	 * 去除list中的重复元素
	 * @param list
     */
	public static List<String> removeDuplicateWithOrder(ArrayList<String> list) {
		Set set = new HashSet<String>();
		ArrayList<String> newList = new ArrayList<String>();
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			String element = (String) iter.next();
			if (set.add(element))
				newList.add(element);
		}
		list.clear();
		list.addAll(newList);
		System.out.println( " remove duplicate " + list);
		return  list;
	}

	public static BlurryPopupWindow blurryPopView(Context mContext, View v) {
		BlurryPopupWindow window;
		window = new BlurryPopupWindow(v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
		// 使窗口里面的空间显示其相应的效果，比较点击button时背景颜色改变。
		// 如果为false点击相关的空间表面上没有反应，但事件是可以监听到的。
		// listview的话就没有了作用。
		window.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
		window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		window.setAnimationStyle(R.style.AnimationPreview);
		window.setFocusable(false);
		window.setOutsideTouchable(false);
//		window.setBackgroundDrawable(new BitmapDrawable());
//		WindowManager.LayoutParams lp = getWindow().getAttributes();
//		getWindow().setAttributes(lp);
		ColorDrawable dw = new ColorDrawable(0x00000000);
//		// 设置SelectPicPopupWindow弹出窗体的背景
		window.setBackgroundDrawable(dw);
		window.update();
		window.showAtLocation(v, Gravity.CENTER, 0, -100);
		return window;
	}

	/**
	 * 显示联系人
	 * @param
     * @param activity
     */
	public static void showDialog(Activity activity,List<HhkqLhAliasBean.DataBean> data,HomePresenter.PopuWindowsClick popuWindowsClick){
		View  view= ArmsUtils.inflate(activity, R.layout.home_popuwind_view);
		RecyclerView recyclerView=view.findViewById(R.id.home_popuwindow_RecyclerView);
		HomepopuwindowAdaptert homepopuwindowAdapter=new HomepopuwindowAdaptert(new ArrayList<>());
		recyclerView.setLayoutManager(new LinearLayoutManager(activity));
		recyclerView.setAdapter(homepopuwindowAdapter);
		final	BlurryPopupWindow pw=blurryPopView(activity,view);
        homepopuwindowAdapter.setNewData(data);
		homepopuwindowAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				popuWindowsClick.clickItem(data.get(position),position);
				pw.dismiss();
			}
		});
	}

	/**
	 * 显示基本信息弹出框
	 * @param context
	 * @param
	 * @param display
     */
	public static void showBaseInfoDialog(Context context, Display display){
//		LayoutInflater lay = (LayoutInflater) context
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		View v = lay.inflate(R.layout.baseinfo_dialog, null);
//		final	BlurryPopupWindow pw=blurryPopView(context,v);
//		TextView close=(TextView) v.findViewById(R.id.linkmanInfo_dialog_close);
//		TextView  linkmanInfo_dialog_stationname=(TextView) v.findViewById(R.id.linkmanInfo_dialog_stationname);
//		TextView  linkmanInfo_dialog_conotent=(TextView) v.findViewById(R.id.linkmanInfo_dialog_conotent);
//		linkmanInfo_dialog_stationname.setText(info.adcdName);
//		linkmanInfo_dialog_conotent.setText("      "+info.remark);
//		close.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				pw.dismiss();
//			}
//		});
	}


}
